package view
{
	import flash.events.Event;
	
	import model.Salle;
	import model.proxy.SalleProxy;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import view.components.SallePanel;
	
	public class SallePanelMediator extends Mediator implements IMediator 
	{ 
		
		public static const NAME : String = 'SallePanelMediator'; 
			
		/** 
		 * Constructor 
		 * Listen event of the component to send notification 
		 */ 
		 public function SallePanelMediator(viewComponent : SallePanel) 
		 { 
		 	super(NAME, viewComponent); 
		 	viewComponent.addEventListener(SallePanel.OBTENIR_LISTE_SALLES, obtenirListeSallesNotification);
		 	viewComponent.addEventListener(SallePanel.VALIDER_AJOUT_SALLE, validerAjoutSalleNotification);	
		 	viewComponent.addEventListener(SallePanel.VALIDER_MODIF_SALLE, validerModifSalleNotification);
		 	viewComponent.addEventListener(SallePanel.SUPPRIMER_SALLE, supprimerSalleNotification);
		 } 
		 
		 
		 private function obtenirListeSallesNotification(event : Event=null):void 
		 { 
			 sendNotification(SallePanel.OBTENIR_LISTE_SALLES, null); 
		 } 
		 
		 /** 
		 * Send a formation notification with a formationVO loaded with the values entered 
		 */ 
		 private function validerAjoutSalleNotification(event : Event=null):void 
		 { 
			 var salle:Salle = new Salle(); 
			 salle.nom = sallePanel.nom.text;
			 salle.capacite = parseInt(sallePanel.capacite.text); 
			 sendNotification(SallePanel.VALIDER_AJOUT_SALLE, salle); 
		 } 
		 
		 private function validerModifSalleNotification(event : Event=null):void 
		 { 
			 var salle:Salle = sallePanel.affichage.selectedItem.valueOf();
			 salle.nom = sallePanel.nom.text;
			 salle.capacite = parseInt(sallePanel.capacite.text); 
			 sendNotification(SallePanel.VALIDER_MODIF_SALLE, salle); 
		 } 
		 
		 private function supprimerSalleNotification(event : Event=null):void
		 {
		 	var listeSalles:Array = sallePanel.affichage.selectedItems;
		 	sendNotification(SallePanel.SUPPRIMER_SALLE, listeSalles);
		 }
		 
		 /** 
		 * List of the notifications catched by this mediator 
		 */ 
		 override public function listNotificationInterests():Array 
		 { 
		 	return [ SalleProxy.LISTE_SALLES_OBTENUE ]; 
		 } 
		 
		 /** 
		 * Actions done when a notifications is catched 
		 */ 
		 override public function handleNotification(note : INotification):void 
		 { 
			 switch (note.getName()) 
			 { 
			 	case SalleProxy.LISTE_SALLES_OBTENUE:
			 	{
			 		sallePanel.affichage.dataProvider = (note.getBody() as Array);	
			 		break;
			 	}
			 	
			 	default:
			 	break;
		 	} 
		 } 
		 
		 /** 
		 * Get the loginPanel component 
		 */ 
		 private function get sallePanel():SallePanel 
		 { 
		 	return viewComponent as SallePanel; 
		 }
	}
}	